package com.dycx.food.converter;

import java.time.LocalDateTime;

import cn.hutool.core.bean.BeanUtil;
import com.dycx.food.enums.OrderStatusEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.food.entity.OrderDO;
import com.dycx.food.vo.OrderVO;
import com.dycx.food.dto.OrderDTO;

/**
 * 商城订单表(Order)实体转换类
 *
 * @author wuzongbing
 * @since 2025-03-27 00:59:46
 */


@Data
public class OrderConverter implements Serializable {
    public static OrderDO dtoToEntity(OrderDTO dto) {
        if (dto == null) return null;
        OrderDO entity = BeanUtil.toBean(dto, OrderDO.class);
        return entity;
    }

    public static OrderVO entityToVo(OrderDO entity) {
        if (entity == null) return null;
        OrderVO vo = BeanUtil.toBean(entity, OrderVO.class);

        OrderStatusEnum orderStatusEnum = OrderStatusEnum.converter(vo.getOrderStatus());
        assert orderStatusEnum != null;
        vo.setOrderStatusName(orderStatusEnum.getName());
        vo.setOrderStatusStyle(orderStatusEnum.getStyle());

        return vo;
    }

    public static List<OrderVO> entityToVoList(List<OrderDO> entityList) {
        List<OrderVO> voList = new ArrayList<>();
        for (OrderDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<OrderVO> entityToVoPage(Page<OrderDO> page) {

        List<OrderVO> newRecords = new ArrayList<>();

        List<OrderDO> records = page.getRecords();
        for (OrderDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<OrderVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
